package cn.edu.jxau.test;

import java.util.Iterator;

import cn.edu.jxau.util.Queue;

/**
 * 3-sum(假设数组递增有序，数组中各个整数互不相同)
 * @author 付大石
 */
public class Main {

    public static void main(String[] args) {

        int[] arr = { -4, -2, -1, 0, 3, 4, 5, 6, 7, 8, 9, 10 };
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                int index = 0;
                if ((index = binarySearch(arr, -(arr[i] + arr[j]))) > j) {
                    System.out.printf("%d\t%d\t%d\n", arr[i], arr[j], arr[index]);
                }
            }
        }
    }

    private static int binarySearch(int[] arr, int key) {

        int low = 0;
        int high = arr.length - 1;
        while (low <= high) {
            int mid = (low + high) / 2;
            if (arr[mid] == key) {
                return mid;
            } else if (arr[mid] < key) {
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        return -1;
    }
}
